#pragma once

#include <iostream>
#include <vector>
#include <deque>
#include <assert.h>
using namespace std;

namespace TL
{
	template<class T>
	struct greater
	{
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T>
	struct less
	{
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T, class container = vector<T>, class compare = less<T>>
	class priority_queue
	{
	public:
		void AdjustUp(int child)
		{
			compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (!com(con[child], con[parent]))
				{
					std::swap(con[child], con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			con.push_back(x);
			AdjustUp(size() - 1);
		}

		void AdjustDown(int parent)
		{
			compare com;
			int child = parent * 2 + 1;
			while (child < size())
			{
				if (child + 1 < size() && !com(con[child + 1], con[child]))
				{
					child++;
				}
				if (!com(con[child], con[parent]))
				{
					std::swap(con[child], con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void pop()
		{
			assert(!empty());
			std::swap(con[0], con[size() - 1]);
			con.pop_back();
			AdjustDown(0);
		}

		size_t size()
		{
			return con.size();
		}

		bool empty()
		{
			return con.empty();
		}

		const T& top()
		{
			return con[0];
		}

		void swap(priority_queue<T>& pi)
		{
			con.swap(pi.con);
		}

	private:
		container con;
	};
	template<class T>
	void swap(priority_queue<T>& pi1, priority_queue<T>& pi2)
	{
		pi1.swap(pi2);
	}
}